﻿using Microsoft.EntityFrameworkCore;
using Zhaoxi.TakeaWay.Model;
 

namespace Zhaoxi.TakeaWay.DataAccess
{


    /// <summary>
    /// coefirst 模式, 需要我们手动创建的
    /// 
    /// 链接数据库的核心
    /// </summary>
    public class TakeaWayDbContext : DbContext
    {
        public TakeaWayDbContext()
        {
        }

        public TakeaWayDbContext(DbContextOptions<TakeaWayDbContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Category> Categories { get; set; }

        public virtual DbSet<Product> Products { get; set; }

        public virtual DbSet<RotationChart> RotationCharts { get; set; }


        /// <summary>
        /// 配置链接
        /// 1. 数据库连接字符串
        /// 2.....
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer("Data Source=47.109.25.112;Initial Catalog=TakeaWayDb;User ID=sa;Password=Sa123456;TrustServerCertificate=true");
            }
        }


        /// <summary>
        /// 配置实体和数据库表之间的映射关系
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Category>(entity =>
            {
                entity.ToTable("Category");
            });
            modelBuilder.Entity<Product>(entity =>
            {
                entity.ToTable("Product");
            });
            modelBuilder.Entity<RotationChart>(entity =>
            {
                entity.ToTable("RotationChart");
            });
        }


    }
}